<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
<form>
    <p>
        <!-- 上传单个文件 -->
        <!-- <input type="file" name="upload_img"> -->
        <!-- 上传多个文件 -->
        <input type="file" multiple name="upload_img">
    </p>
    <p>
        <input type="text" name="user">
    </p>
    <button>提交</button>
</form>

<script>
let form = document.querySelector('form')

form.onsubmit = function (e){
    // 阻止表单提交
    e.preventDefault()

    // 创建FormData对象
    let fd = new FormData(form)

    // 使用js提交
    let xhr = new XMLHttpRequest()
    xhr.open('post','http://localhost:8080/goods/uploads')
    xhr.send(fd)// 发送FormData数据时，请求头自动设置为：Content-Type: multipart/form-data;
    xhr.onreadystatechange = function (){
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                console.log( xhr.responseText )
            } else {
                console.log( xhr.status )
            }
        }
    }
}

</script>

</body>
</html>